import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View, Alert, NetInfo
} from 'react-native';
import { Provider } from 'react-redux';
import { AppState } from './app/app';
//导入store
import { store } from './Redux/router.redux';
//导入网络action
import * as NetInfoAction from './components/NetInfo/net.info.action';
import JPushModule from 'jpush-react-native';
import { NavigationActions } from 'react-navigation';

export default class Router extends Component {
    componentWillMount() {
        this.changeNetInfo();
        JPushModule.notifyJSDidLoad((resultCode) => {
            if (resultCode === 0) {
            }
        });
        JPushModule.addReceiveNotificationListener((map) => {
        });
    }

    componentWillUnmount() {
        store.dispatch(this.resetAction());
        this.removeNetInfo();
    }

    resetAction() {
        return (NavigationActions.reset({
            index: 0,
            actions: [
                NavigationActions.navigate({
                    routeName: 'Index',
                })
            ]
        }));
    }


    removeNetInfo() {
        NetInfo.removeEventListener(
            'connectionChange',
            this._handleConnectionInfoChange
        );
    }

    changeNetInfo() {
        NetInfo.addEventListener(
            'connectionChange',
            this._handleConnectionInfoChange
        );
    }

    _handleConnectionInfoChange(e) {
        store.dispatch(NetInfoAction.NetType(e.type));
    }

    render() {
        return (
            <Provider store={store}>
                <AppState />
            </Provider>
        );
    }
}
